package com.fengxun.software_cup.service;
// 团   队: 风讯网络
// 后   端: Dopa
// 开发时间: 2021/6/14 19:21

import com.fengxun.software_cup.mapper.UserMapper;
import com.fengxun.software_cup.pojo.*;
import com.fengxun.software_cup.util.WordUtil;
import com.fengxun.software_cup.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@Slf4j
@Service
public class UserService {

    @Value("${filePath}")
    String filePath;
    @Value("${pythonPath}")
    String pythonPath;
    @Resource
    UserMapper mapper;

    List<String> list;

    /**
     * 1.上传文件/图片
     * 2.调用python
     *
     * @param files 用户上传的文档
     * @return 状态码、原因
     */
    public CommonResult<Object> putFiles(MultipartFile[] files) {


        list = new ArrayList<>();
        String file_path;
        int state = 0;
        if (files.length==0) {
            return new CommonResult<>(400, "上传失败");
        }

        File file1 = new File(filePath);
        if (!file1.exists()) {
            if (!file1.mkdir()) {
                return new CommonResult<>(400, "文件夹创建失败");
            }
        }

        for (MultipartFile file : files
        ) {
            String fileName = file.getOriginalFilename();
            log.info("上传的文件名：" + fileName);
            try {
                //解析图片
                if (Objects.requireNonNull(file.getContentType()).matches("image.*")) {
                    list.add(CommonUtil.getImageBase(file));
                    continue;
                }
                //保存文件
                file_path = filePath + fileName ;
                File file2 = new File(file_path);
                file.transferTo(file2);
                state = 1;


                //Python解析文件
                int result=CommonUtil.transferPython(pythonPath, file_path);
                if (result!=0){
                    log.info("python:"+result);
                    return new CommonResult<>(500,"python failed");
                }

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
                return new CommonResult<>(500, "IOException");
            }
        }
        return CommonUtil.getResult(mapper,state);
    }

    /**
     * 生成报告，前端下载
     *
     * @param response HttpServletResponse响应
     */
    public void generateReport( HttpServletResponse response) {
        Map<String, Object> map = mapper.getMessage();
        for (int i = 0; i < list.size(); i++) {
            String imageName = "image" + i;
            map.put(imageName, list.get(i));
        }
        try {
            WordUtil.exportMillCertificateWord(response, map, "software_cup", "template.ftl");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
